import { defineStore } from 'pinia'
export const useViewStore = defineStore({
  // id 是必填的，并且所有 Store 中唯一。因为Pinia会将它在devtools显示
  id: 'ViewStore',
  state: () => ({
    TagViews: [] as any[], // 应用内页签
    VWindowViews: [] as any[], // 应用内窗口
    NWindowViews: [] as any[], //新弹出窗口
    activeMap: {},
    menuAry: [],
    menuTree: [],
    menuKV: {},
    modules: {},
  }),
  getters: {},
  actions: {
    CLEAR_VIEWS: function () {
      this.TagViews = []
      this.VWindowViews = []
      this.NWindowViews = []
      this.activeMap = {}
      this.menuKV = {}
      this.menuAry = []
      this.menuTree = []
      //this.modules={};
      return new Promise((resolve) => {
        resolve(true)
      })
    },
    SET_MENU: function (menuAry: any) {
      this.menuAry = menuAry
    },
    SET_TREE: function (menuTree: any) {
      this.menuTree = menuTree
    },
    GET_MENU: function () {
      return this.menuAry
    },
    GET_TREE: function () {
      return this.menuTree
    },
    SET_KV: function (menuKV: any) {
      this.menuKV = menuKV
    },
    GET_KV: function (): any {
      return this.menuKV
    },
    SET_ACT: function (map: any) {
      this.activeMap = map
    },
    GET_ACT: function (): any {
      return this.activeMap
    },
    GET_MODULES: function (): any {
      return this.modules
    },
    CACHE_MODULES(modules: any) {
      this.modules = { ...this.modules, ...modules }
    },
    GET_TAG_VIEWS: function (): any {
      return this.TagViews
    },
    ADD_TAG_VIEWS: function (view: any) {
      if (this.TagViews.some((v: any) => v.path === view.path)) return
      this.TagViews.push(view)
    },
    DEL_TAG_VIEWS: function (view: any) {
      for (const [i, v] of this.TagViews.entries()) {
        if (v.path === view.path) {
          this.TagViews.splice(i, 1)
          break
        }
      }
      return new Promise((resolve) => {
        resolve(this.TagViews)
      })
    },
    ADD_VW_VIEWS: function (view: any) {
      if (this.VWindowViews.some((v: any) => v.path === view.path)) return
      this.VWindowViews.push(view)
    },
    GET_VW_VIEWS: function (): any {
      return this.VWindowViews
    },
    DEL_VW_VIEWS: function (view: any) {
      for (const [i, v] of this.VWindowViews.entries()) {
        if (v.path === view.path) {
          this.VWindowViews.splice(i, 1)
          break
        }
      }
      return new Promise((resolve) => {
        resolve(this.VWindowViews)
      })
    },
    ADD_NW_VIEWS: function (view: any) {
      if (this.NWindowViews.some((v: any) => v.path === view.path)) return
      this.NWindowViews.push(view)
    },
    GET_NW_VIEWS: function (): any {
      return this.NWindowViews
    },
    DEL_NW_VIEWS: function (view: any) {
      for (const [i, v] of this.NWindowViews.entries()) {
        if (v.path === view.path) {
          this.NWindowViews.splice(i, 1)
          break
        }
      }
      return new Promise((resolve) => {
        resolve(this.NWindowViews)
      })
    },
    DEL_ALL_VIEWS: function () {
      this.TagViews = []
      this.VWindowViews = []
      this.NWindowViews = []
    },
    DEL_OTHERS_VIEWS: function (view: any) {
      for (const [i, v] of this.TagViews.entries()) {
        if (v.path === view.path) {
          this.TagViews = this.TagViews.slice(i, i + 1)
          break
        }
      }
      return new Promise((resolve) => {
        resolve(this.TagViews)
      })
    },
  },
})
